#include "LsdInPro.h"
#include "LsdMem.h"
#include "LsdLinkList.h"
#include "LsdEvent.h"
#include "main.h"
#include "hal_tds.h"
#include "LsdEEPROM.h"
#include "hal_temper.h"
#include "bsp_adc.h"
#include "hal_flow.h"
#include "hal_InCheck.h"
#include "bsp.h"
static tsInputCtrl inputCtrl;
static void OnCheckHighGasSwitch(void);// 高压CO2开关
static void OnCheckHeatHigh(void); 	//热罐高水位
static void OnCheckHeatMid(void); 	//热罐中水位
static void OnCheckHeatLow(void); 	//热罐低水位
static void OnCheckCoolHigh(void); 	//冷罐高水位
static void OnCheckCoolLow(void); 	//冷罐低水位
static void OnChecSodaHigh(void); 	//苏达罐高水位
#if (DEV_HARD_VER<=10001) 
#else
static void OnChecSodaLow(void); 	//苏达罐低水位
#endif

uint8_t OnGetIOStatus (uint32_t i);

static void OnCheckConfirm(uint32_t type);
static uint16_t OnGetHighGasSwStatus(void) //高压CO2开关
{
	uint16_t ret=HAL_GPIO_ReadPin(SW_CO2_PRESSURE_GPIO_Port, SW_CO2_PRESSURE_Pin);
	return ret;
}
static  uint16_t OnGetHeatHighValue(void)  	//热罐高
{
	uint16_t ret=OnHalTdsADCAverValue(TDS_HEAT_H);
	return ret;
}
static  uint16_t OnGetHeatMidValue(void) 		//热罐中
{
	uint16_t ret=OnHalTdsADCAverValue(TDS_HEAT_M);
	return ret;
}
static  uint16_t OnGetHeatLowValue(void)		//热罐低
{
	uint16_t ret=OnHalTdsADCAverValue(TDS_HEAT_L);
	return ret;
}
static  uint16_t OnGetCoolHighValue(void)  	//冷罐高
{
	uint16_t ret=OnHalTdsADCAverValue(TDS_COOL_H);
	return ret;
}
static  uint16_t OnGetCoolLowValue(void)		//冷罐低
{
	uint16_t ret=OnHalTdsADCAverValue(TDS_COOL_L);
	return ret;
}
	static  uint16_t OnGetSodaHighValue(void)		//苏达罐高
{
	uint16_t ret=OnHalTdsADCAverValue(TDS_SODA_H);
	return ret;
}
#if (DEV_HARD_VER<=10001) 
#else
static  uint16_t OnGetSodaLowValue(void)		//苏达罐低
{
	uint16_t ret=OnHalTdsADCAverValue(TDS_SODA_L);
	return ret;
}
#endif	


//错误参数配置
static tsInputParam paramTable[TYPE_INPUT_MAX] = {

	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
	{START_KEEP, END_KEEP},
};

//错误事件配置表
static const tsInputFunc funcTable[] = {
	
	{OnCheckHighGasSwitch, 	OnCheckConfirm		,	OnGetHighGasSwStatus,							1},
	{OnCheckHeatHigh,			 	OnCheckConfirm		,	OnGetHeatHighValue,				    		4000},
	{OnCheckHeatMid,				OnCheckConfirm		,	OnGetHeatMidValue,								4020},
	{OnCheckHeatLow, 			 	OnCheckConfirm		,	OnGetHeatLowValue,								4000},
	{OnCheckCoolHigh, 			OnCheckConfirm		,	OnGetCoolHighValue,								4050},
	{OnCheckCoolLow, 				OnCheckConfirm		,	OnGetCoolLowValue,								4000},
	{OnChecSodaHigh, 				OnCheckConfirm		,	OnGetSodaHighValue,								4000},
#if (DEV_HARD_VER<=10001) 	
	{NULL, 									NULL					 		,	NULL,															0},
#else
	{OnChecSodaLow, 				OnCheckConfirm		,	OnGetSodaLowValue,								4000},
#endif	
	{NULL, 									NULL					 		,	NULL,															0},
	{NULL, 									NULL					 		,	NULL,															0},
	{NULL, 									NULL					 		,	NULL,															0},	
	{NULL, 									NULL					 		,	NULL,															0},
	{NULL, 									NULL					 		,	NULL,															0},
	{NULL, 									NULL					 		,	NULL,															0},
	{NULL, 									NULL					 		,	NULL,															0},
};
//判定
static void OnCheckHighGasSwitch(void)// 高压CO2开关
{
	paramTable[TYPE_PRESSURE_HIGH_CO2_SW].start_keep  = CNT_1S_KEEP;
	paramTable[TYPE_PRESSURE_HIGH_CO2_SW].end_keep    = CNT_1S_KEEP;
	if(funcTable[TYPE_PRESSURE_HIGH_CO2_SW].pfn_getstatus() == funcTable[TYPE_PRESSURE_HIGH_CO2_SW].polarity)
	{
		 inputCtrl.inst |= (1<<TYPE_PRESSURE_HIGH_CO2_SW);
	}
	else
	{
		 inputCtrl.inst &= ~(1<<TYPE_PRESSURE_HIGH_CO2_SW);
	}
}
static void OnCheckHeatHigh(void) //热罐高水位
{
	  if(powerDownSave.work_mode == 1) //测试模式
		{
				paramTable[TYPE_WATER_HIGH_HEAT].start_keep = 0; //浮子再没到低液位时就动作了
				paramTable[TYPE_WATER_HIGH_HEAT].end_keep   = 0;
				if(funcTable[TYPE_WATER_HIGH_HEAT].pfn_getstatus() <= funcTable[TYPE_WATER_HIGH_HEAT].polarity)
				{
					 inputCtrl.inst |= (1<<TYPE_WATER_HIGH_HEAT);
				}
				else
				{
					 inputCtrl.inst &= ~(1<<TYPE_WATER_HIGH_HEAT);
				}
		}
		else
		{
				paramTable[TYPE_WATER_HIGH_HEAT].start_keep	= CNT_2S_KEEP; //浮子再没到低液位时就动作了
				paramTable[TYPE_WATER_HIGH_HEAT].end_keep 	= CNT_5S_KEEP;
			  if(funcTable[TYPE_WATER_HIGH_HEAT].pfn_getstatus() <= funcTable[TYPE_WATER_HIGH_HEAT].polarity)
				{
					 inputCtrl.inst |= (1<<TYPE_WATER_HIGH_HEAT);
				}
				else
				{
					 inputCtrl.inst &= ~(1<<TYPE_WATER_HIGH_HEAT);
				}
		}
}
static void OnCheckHeatMid(void) //热罐中水位
{
		if(powerDownSave.work_mode == 1) //测试模式
		{
			paramTable[TYPE_WATER_MID_HEAT].start_keep	= 0;
			paramTable[TYPE_WATER_MID_HEAT].end_keep		= 0;
		}
		else
		{
			paramTable[TYPE_WATER_MID_HEAT].start_keep	= CNT_5S_KEEP;
			paramTable[TYPE_WATER_MID_HEAT].end_keep		= CNT_5S_KEEP;
		}
		//必须是低液位以上 同时在中液位以上
		if(funcTable[TYPE_WATER_MID_HEAT].pfn_getstatus() <= funcTable[TYPE_WATER_MID_HEAT].polarity)//&& (funcTable[TYPE_WATER_LOW_HEAT].pfn_getstatus() <= funcTable[TYPE_WATER_LOW_HEAT].polarity)
		{
			 inputCtrl.inst |= (1<<TYPE_WATER_MID_HEAT);
		}
		else
		{
			 inputCtrl.inst &= ~(1<<TYPE_WATER_MID_HEAT);
		}
}
static void OnCheckHeatLow(void) //热罐低水位
{
	  if(powerDownSave.work_mode == 1) //测试模式
		{
			paramTable[TYPE_WATER_LOW_HEAT].start_keep	= 0;
			paramTable[TYPE_WATER_LOW_HEAT].end_keep		= 0;
		}
		else if(powerDownSave.work_mode == 0)//正常模式
		{
			paramTable[TYPE_WATER_LOW_HEAT].start_keep	= CNT_1S_KEEP;
			paramTable[TYPE_WATER_LOW_HEAT].end_keep		= CNT_10S_KEEP;
		}
    else//其他模式
		{
			paramTable[TYPE_WATER_LOW_HEAT].start_keep	= CNT_1S_KEEP;
			paramTable[TYPE_WATER_LOW_HEAT].end_keep		= CNT_1S_KEEP;
		}			

	 if(funcTable[TYPE_WATER_LOW_HEAT].pfn_getstatus() >= funcTable[TYPE_WATER_LOW_HEAT].polarity)
	{
		 inputCtrl.inst |= (1<<TYPE_WATER_LOW_HEAT);
	}
	else
	{
		 inputCtrl.inst &= ~(1<<TYPE_WATER_LOW_HEAT);
	}
}
static void OnCheckCoolHigh(void)//冷罐高水位
{
		if(powerDownSave.work_mode == 1) //测试模式
		{
			paramTable[TYPE_WATER_HIGH_COOL].start_keep	= 0;
			paramTable[TYPE_WATER_HIGH_COOL].end_keep		= 0;
		}
		else
		{
			paramTable[TYPE_WATER_HIGH_COOL].start_keep	= CNT_1S_KEEP;
			paramTable[TYPE_WATER_HIGH_COOL].end_keep		= CNT_15S_KEEP;
//			paramTable[TYPE_WATER_HIGH_COOL].start_keep= 1;
//			paramTable[TYPE_WATER_HIGH_COOL].end_keep= 1;
		}
		//必须是低液位以上 同时在中液位以上
		if(funcTable[TYPE_WATER_HIGH_COOL].pfn_getstatus() <= funcTable[TYPE_WATER_HIGH_COOL].polarity)//&& (funcTable[TYPE_WATER_LOW_HEAT].pfn_getstatus() <= funcTable[TYPE_WATER_LOW_HEAT].polarity)
		{
			 inputCtrl.inst |= (1<<TYPE_WATER_HIGH_COOL);
		}
		else
		{
			 inputCtrl.inst &= ~(1<<TYPE_WATER_HIGH_COOL);
		}
}
static void OnCheckCoolLow(void) //冷罐低水位
{
	  if(powerDownSave.work_mode == 1) //测试模式
		{
			paramTable[TYPE_WATER_LOW_COOL].start_keep	= 0;
			paramTable[TYPE_WATER_LOW_COOL].end_keep		= 0;
		}
		else if(powerDownSave.work_mode == 0)//正常模式
		{
			paramTable[TYPE_WATER_LOW_COOL].start_keep	= CNT_1S_KEEP;
			paramTable[TYPE_WATER_LOW_COOL].end_keep	= CNT_1S_KEEP;
		}
    else//其他模式
		{
			paramTable[TYPE_WATER_LOW_COOL].start_keep	= CNT_1S_KEEP;
			paramTable[TYPE_WATER_LOW_COOL].end_keep		= CNT_1S_KEEP;
		}			

	 if(funcTable[TYPE_WATER_LOW_COOL].pfn_getstatus() >= funcTable[TYPE_WATER_LOW_COOL].polarity)
	{
		 inputCtrl.inst |= (1<<TYPE_WATER_LOW_COOL);
	}
	else
	{
		 inputCtrl.inst &= ~(1<<TYPE_WATER_LOW_COOL);
	}
}
static void OnChecSodaHigh(void) //苏达罐高水位
{
		if(powerDownSave.work_mode == 1) //测试模式
		{
			paramTable[TYPE_WATER_HIGH_SODA].start_keep= 0;
			paramTable[TYPE_WATER_HIGH_SODA].end_keep= 0;
		}
		else
		{
			paramTable[TYPE_WATER_HIGH_SODA].start_keep= CNT_5S_KEEP;
			paramTable[TYPE_WATER_HIGH_SODA].end_keep= CNT_2S_KEEP;
		}
		//必须是低液位以上 同时在中液位以上
		if(funcTable[TYPE_WATER_HIGH_SODA].pfn_getstatus() <= funcTable[TYPE_WATER_HIGH_SODA].polarity)//&& (funcTable[TYPE_WATER_LOW_HEAT].pfn_getstatus() <= funcTable[TYPE_WATER_LOW_HEAT].polarity)
		{
			 inputCtrl.inst |= (1<<TYPE_WATER_HIGH_SODA);
		}
		else
		{
			 inputCtrl.inst &= ~(1<<TYPE_WATER_HIGH_SODA);
		}
}
#if (DEV_HARD_VER<=10001) 	
#else
static void OnChecSodaLow(void) //苏达罐低水位
{
		if(powerDownSave.work_mode == 1) //测试模式
		{
			paramTable[TYPE_WATER_LOW_SODA].start_keep= 0;
			paramTable[TYPE_WATER_LOW_SODA].end_keep= 0;
		}
		else
		{
			paramTable[TYPE_WATER_LOW_SODA].start_keep= CNT_1S_KEEP;
			paramTable[TYPE_WATER_LOW_SODA].end_keep= CNT_1S_KEEP;
		}
		//必须是低液位以上 同时在中液位以上
		if(funcTable[TYPE_WATER_LOW_SODA].pfn_getstatus() >= funcTable[TYPE_WATER_LOW_SODA].polarity)//&& (funcTable[TYPE_WATER_LOW_HEAT].pfn_getstatus() <= funcTable[TYPE_WATER_LOW_HEAT].polarity)
		{
			 inputCtrl.inst |= (1<<TYPE_WATER_LOW_SODA);
		}
		else
		{
			 inputCtrl.inst &= ~(1<<TYPE_WATER_LOW_SODA);
		}
}
#endif
static void OnCheckConfirm(uint32_t type)
{
		uint32_t mask = 1<<type;
		if((inputCtrl.inst & mask) !=0)   //@7检测到有置位
		{
			if((inputCtrl.conf.value & mask) ==0)
				{
						if(inputCtrl.timer[type]++<paramTable[type].start_keep)
							goto CONF_EXIT;
						inputCtrl.conf.value |=mask;
				}
		}
		else                         //@7 没有置位
		{
			if(((inputCtrl.conf.value & mask) !=0) && (type >= TYPE_PRESSURE_HIGH_CO2_SW)) //@7 之前有确认的时&& (type >= TYPE_PRESSURE_LOW) 这个之前的是不可恢复 比低压开关大的是可以恢复的
			{
				 if( inputCtrl.timer[type]++ < paramTable[type].end_keep )
						goto CONF_EXIT;
				 inputCtrl.conf.value &=~mask; 
			}
		
		}
		inputCtrl.timer[type] = 0;  //什么都没有就把计数清0
CONF_EXIT:
			mask=0;
}
uint8_t OnGetIOStatus (uint32_t i)
{  
	 if((inputCtrl.conf.value & (0x00000001<<i) )!=0)
		 return 1;
	 else
		 return 0;
}

void OnHalInCheck(void)  //1000ms
{
	for(uint8_t i=0;i<TYPE_INPUT_MAX;i++)
	{
		if(funcTable[i].pfn_check != NULL)
		funcTable[i].pfn_check();//检查
		if(funcTable[i].pfn_confirm != NULL)
		funcTable[i].pfn_confirm(i);	//确认
	}	
}
